const app = getApp();
const https = require('../../utils/https.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    login_status: true,
    user_info: '',
    wait_pay_count: 0,
    wait_shipping_count: 0,
    wait_common_count: 0,
    over_count: 0,
    inspector_name: '',
    inspector_pass: '',
    showModalStatus: false, // 分享有礼弹窗
  },
  onInfo() {
    wx.navigateTo({
      url: '/pages/my_data/my_data',
    })
  },
  login_status() {
    this.setData({
      maskShow: true,
      signShow: true
    })
  },
  maskHide() {
    this.setData({
      maskShow: false,
      signShow: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * @xk 调用授权，进行登录
   */
  login: function (e) {
    var that = this;
    app.globalData.backurl = '/pages/my/my';
    wx.navigateTo({
      url: '/pages/grant/grant',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log(12223)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this,
      login_status = wx.getStorageSync('login_status');
    that.setData({
      login_status: login_status,
      user_info: []
    })
    that.getUserInfo();
    that.getOrderCount();
    that.getHouseOrderCount();
  },
  /**
   * @xk 获取用户信息
   */
  getUserInfo: function () {
    var that = this,
    login_status = wx.getStorageSync('login_status'),
      user_id = wx.getStorageSync('user_id');
    if (user_id > 0 && login_status) {
      https.post('getUserInfo', {
        user_id: user_id
      }, false, '加载中').then(res => {
        that.setData({
          user_info: res
        })
      })
    }
  },
  /**
   * @xk 获取订单统计数据
   */
  getOrderCount: function () {
    var that = this,
        user_id = wx.getStorageSync('user_id');
    if(user_id > 0){
      https.post('getOrderCount', {user_id: user_id}, true).then(res => {
        that.setData({
          wait_pay_count: res.wait_pay_count,
          wait_shipping_count: res.wait_shipping_count,
          wait_common_count: res.wait_common_count,
          over_count: res.over_count,
          refoundCount: res.refoundCount,
        })
      })
    }

  },
    /**
   * @xk 获取订单统计数据
   */
  getHouseOrderCount: function () {
    var that = this,
        user_id = wx.getStorageSync('user_id');
    if(user_id > 0){
      https.post('getHouseOrderCount', {user_id: user_id}, true).then(res => {
        that.setData({
          wait_check_count: res.wait_check_count,
          check_count: res.check_count,
          over_countx: res.over_count,
        })
      })
    }

  },
  /**
   * @xk 点击验房受理
   */
  checkBookHouse: function () {
    var that = this,
      login_status = wx.getStorageSync('login_status'),
      is_login_inspector = wx.getStorageSync('is_login_inspector');
      if (!login_status || login_status <= 0) {
        wx.showLoading({
          title: '请先进行登陆',
        })
        setTimeout(() => {
          wx.hideLoading({
            success: (res) => {
              wx.navigateTo({
                url: '/pages/grant/grant',
              })
            },
          })
        }, 1500);
      }else{
        if (is_login_inspector && is_login_inspector == 1) {
          //已完成验房师登陆
          // 执行跳转页面
          wx.navigateTo({
            url: '/pages/inspection_accept/accept',
          })
        } else {
          that.setData({
            login_type: 'checkBookHouse',
            maskShow: true,
            signShow: true,
          })
        }
      }

  },
  /**
   * @xk 点击受理大厅
   */
  bookListing: function () {
    var that = this,
      login_status = wx.getStorageSync('login_status'),
      is_login_inspector = wx.getStorageSync('is_login_inspector');
      if (!login_status || login_status <= 0) {
        wx.showLoading({
          title: '请先进行登陆',
        })
        setTimeout(() => {
          wx.hideLoading({
            success: (res) => {
              wx.navigateTo({
                url: '/pages/grant/grant',
              })
            },
          })
        }, 1500);
      }else{
        if (is_login_inspector && is_login_inspector == 1) {
          //已完成验房师登陆
          // 执行跳转页面
          wx.navigateTo({
            url: '/pages/inspection_acceptance/acceptance',
          })
        } else {
          that.setData({
            login_type: 'bookListing',
            maskShow: true,
            signShow: true,
          })
        }
      }

  },
  /**
   * @xk 用户名获取
   * @param {*} obj
   */
  chanageInspectorName: function (obj) {
    this.setData({
      inspector_name: obj.detail.value
    })
  },
  chanageInspectorPass: function (obj) {
    this.setData({
      inspector_pass: obj.detail.value
    })
  },
  /**
   * @xk 验房师登陆
   * @param {} obj
   */
  Login: function (obj) {
    var that = this,
      inspector_name = this.data.inspector_name,
      login_type = this.data.login_type,
      inspector_pass = this.data.inspector_pass;
    if (inspector_name == '') {
      wx.showToast({
        title: '请输入用户名',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (inspector_pass == '') {
      wx.showToast({
        title: '请输入登陆密码',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    https.post('checkInspectorLogin', {
      inspector_name: inspector_name,
      inspector_pass: inspector_pass
    }, false, '正在登陆').then(res => {
      if (res > 0) {
        wx.setStorageSync('is_login_inspector', 1);
        wx.setStorageSync('inspector_id', res);
        that.setData({
          maskShow: false,
          signShow: false,
        })
        setTimeout(() => {
          switch (login_type) {
            case 'checkBookHouse':
              wx.navigateTo({
                url: '/pages/inspection_accept/accept',
              })
              break;
            case 'bookListing':
              wx.navigateTo({
                url: '/pages/inspection_acceptance/acceptance',
              })
              break;
          }
        }, 1500);
      }
    })
  },
  loginOut: function(){
    let version = wx.getStorageSync('version');
    let app_name = wx.getStorageSync('app_name'),
        app_logo = wx.getStorageSync('app_logo');
    wx.clearStorageSync();
    wx.setStorageSync('version', version);
    wx.setStorageSync('app_name', app_name);
    wx.setStorageSync('app_logo', app_logo);
    app.globalData.user_id = ''
    app.globalData.phone = ''
    app.globalData.openid = ''
    app.globalData.unionId = ''
    app.globalData.token = '';
    this.onShow();
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.hideModal()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var timestamp = Date.parse(new Date()),
    app_name = wx.getStorageSync('app_name'),
    app_logo = wx.getStorageSync('app_logo'),
    user_id = wx.getStorageSync('user_id'),
    url = '/pages/home/home',
    timestr = timestamp / 1000;
  url += '?timestr=' + timestr;
  if (user_id > 0) {
    url += '&l_id=' + user_id;
  }
  return {
    title: app_name,
    path: url,
    imageUrl: app_logo
  }
  },
  showModal: function () {

    var login_status = wx.getStorageSync('login_status');
    if (!login_status || login_status <= 0) {
      wx.showLoading({
        title: '请先进行登陆',
      })
      setTimeout(() => {
        wx.hideLoading({
          success: (res) => {
            wx.navigateTo({
              url: '/pages/grant/grant',
            })
          },
        })
      }, 1500);
      return false;
    }

    var that = this;

    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "ease-in-out",
      delay: 0
    })
    this.animation = animation
    animation.translateY(500).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  // 关闭分享actionsheet
  hideModal: function () {
    this.setData({
      showModalStatus: false,
    })
  },
  // 分享海报
  btnposter: function () {
    if (this.data.ewm_pic) {
      wx.previewMedia({
        sources: [{url: this.data.ewm_pic}] // 需要预览的图片http链接列表
      }, true)
      return
    }
    var that = this;
    // 获取太阳码
    wx.showLoading({
      title: '正在生成海报'
    })
    var  user_id = wx.getStorageSync('user_id');
    https.post('getEwm',{user_id: user_id},true,'正在生成海报').then(res=>{
      if(res){
        that.setData({
          ewm_pic: res
        })
        that.btnposter()
      }
    }).finally(() => {
      wx.hideLoading({
        success: (res) => {},
      })
    })
  }
})
